package com.example.mvvmbase.wedgit;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.mvvmbase.R;

import java.util.ArrayList;
import java.util.List;

public class TabLayout extends LinearLayout implements View.OnClickListener {

    private List<View> mTabViews;
    private List<TabItem> mTabs;
    private OnTabSelectListener mOnTabSelectListener;

    public TabLayout(Context context) {
        super(context);
        init();
    }

    public TabLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TabLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setOnTabSelectListener(OnTabSelectListener listener) {
        mOnTabSelectListener = listener;
    }

    private void init() {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        mTabViews = new ArrayList<>();
        mTabs = new ArrayList<>();
    }

    /**
     * 添加TabItem
     *
     * @param item
     */
    public void addTabItem(TabItem item) {
        View view;
        if (null != item.mText) {
            view = inflate(getContext(), R.layout.tab_item_layout_hybrid, null);
        } else {
            view = inflate(getContext(), R.layout.tab_item_layout_image, null);
        }

        TextView textView = (TextView) view.findViewById(R.id.title);
        ImageView imageView = (ImageView) view.findViewById(R.id.icon);
        imageView.setImageResource(item.mDefaultResId);
        if (textView != null) {
            textView.setText(item.mText);
            textView.setTextColor(item.mDefaultColor);
        }

        view.setTag(mTabViews.size());
        view.setOnClickListener(this);

        mTabViews.add(view);
        mTabs.add(item);

        addView(view);
    }

    /**
     * 设置选中TabItem
     *
     * @param position
     */
    public void setSelectItem(int position) {
        if (position >= mTabs.size() || position < 0) {
            position = 0;
        }

        mTabViews.get(position).performClick();
        updateState(position);
    }

    /**
     * 更新状态
     *
     * @param position
     */
    private void updateState(int position) {
        for (int i = 0; i < mTabViews.size(); i++) {
            View view = mTabViews.get(i);
            TextView textView = (TextView) view.findViewById(R.id.title);
            ImageView imageView = (ImageView) view.findViewById(R.id.icon);
            if (i == position) {
                imageView.setImageResource(mTabs.get(i).mSelectResId);
                if (textView != null)
                    textView.setTextColor(mTabs.get(i).mSelectColor);
            } else {
                imageView.setImageResource(mTabs.get(i).mDefaultResId);
                if (textView != null)
                    textView.setTextColor(mTabs.get(i).mDefaultColor);
            }
        }
    }


    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        if (mOnTabSelectListener != null) {
            mOnTabSelectListener.onTabSelected(v, position);
        }

        updateState(position);
    }

    public interface OnTabSelectListener {
        void onTabSelected(View v, int position);
    }

    public static class TabItem {
        private int mDefaultResId;
        private int mSelectResId;
        private int mDefaultColor;
        private int mSelectColor;
        private String mText;


        public TabItem setText(String text) {
            mText = text;
            return this;
        }

        public TabItem setDefaultDrawable(int res) {
            mDefaultResId = res;
            return this;
        }

        public TabItem setSelectedDrawable(int res) {
            mSelectResId = res;
            return this;
        }

        public TabItem setTextDefaultColor(int color) {
            mDefaultColor = color;
            return this;
        }

        public TabItem setTextCheckedColor(int color) {
            mSelectColor = color;
            return this;
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mTabViews != null) {
            mTabViews.clear();
        }
        if (mTabs != null) {
            mTabs.clear();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        for (int i = 0; i < mTabViews.size(); i++) {
            View view = mTabViews.get(i);
            int width = getResources().getDisplayMetrics().widthPixels / (mTabs.size());
            LayoutParams params = new LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT);

            view.setLayoutParams(params);
        }
    }
}
